package com.lemon.smartsafety.http;


import android.util.Log;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.utils.app.ToastUtils;
import com.orhanobut.logger.Logger;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;


/**
 * 项目名称：exec_law
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/6/17 11:38
 * 修改人：Fargo
 * 修改时间：2016/6/17 11:38
 * 修改备注：
 */
public class HttpOkBase {

    public static void LOGE(final String tag, String message) {

            Log.e(tag, message);

    }

    public static void LOGE(String message) {
            Log.e("Debug:", message);

    }




    public interface HttpInterface {
        public void callBackFunction(boolean httpClient, String result);

    }


    public static void httpBasePost(LemonRequestParams params, final String url, String TAG, final HttpInterface httpInterface) {
        Logger.i("url\n"+url);
        Logger.i("params\n"+params.toString());

        String strTag=url;

        OkHttpUtils.post().url(url).tag(strTag).params(params).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {
                try {
                    LOGE("onError"+url);
                    LOGE("HttpPath:" + url);
                    String msg = "网络请求异常";
                   /*     if (e instanceof SocketTimeoutException) {
                    e = new Exception("请求超时");
                    msg="请求超时";
                }
                if(e == null){
                    msg = "取消请求";
                }*/
                    call.cancel();
                    ToastUtils.toastMsg(msg);
                    httpInterface.callBackFunction(false, msg);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }

            @Override
            public void onResponse(String response) {
                try {
                    LOGE("onResponse");
                    LOGE("onSuccess_HttpPath:" + url);
                    String result=response.trim();
                    LOGE("返回", "内容为:"+result);
                    httpInterface.callBackFunction(true, result);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });


    }


    /**
     * @param params
     * @param httpInterface
     */
    public static void httpBaseGet(LemonRequestParams params, final String url,String TAG, final HttpInterface httpInterface) {


        LOGE("HttpPath:" + url);
        String strTag=url;

        OkHttpUtils.get().url(url).tag(strTag).params(params).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {
                try {
                    LOGE("onError");
                    String msg = "网络请求异常";
           /*     if (e instanceof SocketTimeoutException) {
                    e = new Exception("请求超时");
                    msg="请求超时";
                }
                if(e == null){
                    msg = "取消请求";
                }*/
                    call.cancel();
                    ToastUtils.toastMsg(msg);
                    httpInterface.callBackFunction(false, msg);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }

            @Override
            public void onResponse(String response) {
                try {
                    LOGE("onResponse");
                    LOGE("onSuccess_HttpPath:" + url);
                    String result=response;
                    LOGE("返回", "内容为:"+result);
                    httpInterface.callBackFunction(true, result);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });




    /*    OkHttpUtils.get().url(url).tag("").params(params).build().execute(new lemon.com.exec_law.http.okHttp.okhttp.callback.Callback() {
            @Override
            public Object parseNetworkResponse(Response response) throws IOException {

                LogUtils.LOGE("onSuccess_HttpPath:" + url);
                String result=response.body().string();
                LogUtils.LOGE("返回", result);
                httpInterface.callBackFunction(true, result);

                return null;
            }

            @Override
            public void onError(Call call, Exception e) {
                if (AppApplication.DEBUG) {
                    e.printStackTrace();
                }
                if (e instanceof SocketTimeoutException) {
                    e = new Exception("请求超时");
                }
                if(e == null){
                    e = new NullPointerException("取消请求");
                }
                String msg = e.getLocalizedMessage();
                msg = StringUtil.nullValue(msg);
                if ("Canceled".equalsIgnoreCase(msg)) {
                    msg = "取消请求";
                }else{
                    msg = "请求超时";
                }

                httpInterface.callBackFunction(false, msg);
            }

            @Override
            public void onResponse(Object response) {


            }
        });*/


    }


}
